package cn.harry.modular.oss.controller;

import cn.harry.common.api.R;
import cn.harry.common.exception.ApiException;
import cn.harry.modular.oss.service.impl.OssServiceImpl;
import cn.harry.modular.oss.vo.OssCallbackResult;
import cn.harry.modular.oss.vo.OssPolicyResult;
import cn.hutool.core.util.StrUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Objects;

/**
 * ClassName: OssController Description: Oss相关操作接口
 *
 * @author honghh Date 2019/10/08 14:58 Copyright (C) www.tech-harry.cn
 */

@RestController
@Tag(name = "oss-aliyun => 文件管理")
@RequestMapping("/oss/aliyun")
public class OssController {

	@Resource
	private OssServiceImpl ossService;

	@Operation(summary = "policy 上传签名生成")
	@RequestMapping(value = "/policy", method = RequestMethod.GET)
	public R<OssPolicyResult> policy() {
		OssPolicyResult result = ossService.policy();
		return R.success(result);
	}

	@Operation(summary = "callback 上传成功回调")
	@RequestMapping(value = "callback", method = RequestMethod.POST)
	public R<OssCallbackResult> callback(HttpServletRequest request) {
		OssCallbackResult ossCallbackResult = ossService.callback(request);
		return R.success(ossCallbackResult);
	}

	@Operation(summary = "upload 上传")
	@RequestMapping(value = "upload", method = RequestMethod.POST)
	public R<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
		if (file.isEmpty()) {
			throw new ApiException("上传文件不能为空");
		}
		String suffix = "." + getExtension(file);

		// String suffix =
		// Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));

		String url = ossService.uploadSuffix(file.getBytes(), suffix);

		return R.success(url);
	}

	/**
	 * 获得文件后缀名，扩展名不带“.”
	 * @param file
	 * @return
	 */
	public static String getExtension(MultipartFile file) {
		String extension = FilenameUtils.getExtension(file.getOriginalFilename());
		if (StrUtil.isEmpty(extension)) {
			extension = getExtension(Objects.requireNonNull(file.getContentType()));
		}
		return extension;
	}

	public static final String IMAGE_PNG = "image/png";

	public static final String IMAGE_JPG = "image/jpg";

	public static final String IMAGE_JPEG = "image/jpeg";

	public static final String IMAGE_BMP = "image/bmp";

	public static final String IMAGE_GIF = "image/gif";

	public static String getExtension(String prefix) {
		switch (prefix) {
		case IMAGE_PNG:
			return "png";
		case IMAGE_JPG:
			return "jpg";
		case IMAGE_JPEG:
			return "jpeg";
		case IMAGE_BMP:
			return "bmp";
		case IMAGE_GIF:
			return "gif";
		default:
			return "";
		}
	}

}
